//
//  HJRetweetedView.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/28.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import SnapKit
class HJRetweetedView: UIView {
    
    //MARK: 属性
    
    ///约束属性
    
    var retweetViewCon: Constraint?
    
    
    ///转发微博模型
    
    var retweetStatus: HJStatus?{
        
        didSet{
            ///名字
            guard let screen_name = retweetStatus?.user?.screen_name else{
                
                HJPrint(msg: "screen_name没有值")
                
                return
            }
            ///内容
            guard let text = retweetStatus?.text else{
                
                HJPrint(msg: "text没有值")
                
                return
                
            }
            
            
            
            ///被转发微博里的名字内容
            contentLabel.text = "@\(screen_name): \(text)"
            
            ///判断转发的微博里是否有图片
            ///将原来底部的约束删除
            self.retweetViewCon?.uninstall()
            
            if let pictureUrl = retweetStatus?.picturesURL{
                
                retweetView.hidden = false
                
                retweetView.picturesURL = pictureUrl
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewCon = make.bottom.equalTo(retweetView).offset(HJPictureViewMargin).constraint
                    
                })
                
            }else {
                
                retweetView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewCon = make.bottom.equalTo(contentLabel).offset(HJPictureViewMargin).constraint
                    
                })
                
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func  prepareUI(){
        ///添加子控件
        addSubview(bkgButton)
        addSubview(contentLabel)
        addSubview(retweetView)
        
        ///添加约束
        bkgButton.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self)
            
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            
            make.top.leading.equalTo(self).offset(HJPictureViewMargin)
            make.trailing.equalTo(self).offset(-HJPictureViewMargin)
            
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(HJPictureViewMargin)
            
            make.leading.equalTo(contentLabel)
        }
        
        ///底部约束
        self.snp_makeConstraints { (make) -> Void in
            
            self.retweetViewCon = make.bottom.equalTo(retweetView).offset(HJPictureViewMargin).constraint
            
            
        }
        
        
    }
    
    //MARK: 懒加载
    
    ///转发微博背景
    
    private lazy var bkgButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        
        
        return button
        
    }()
    
    
    
    ///转发微博内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(HJPictureViewFontSize)
        
        label.numberOfLines = 0
        
        label.textColor = UIColor.darkGrayColor()
        
        
        return label
        
    }()
    
    ///转发微博配图
    private lazy var retweetView: HJStatusPictureView = HJStatusPictureView()
    
    
}
